<!-- 更多功能 – 卡券管理 - 弹窗组件 -->
<template>
  <CustomDialog
    v-model:visible="dialogVisible"
    top="5vh"
    width="80%"
    curent-class="coupon-management-dialog"
  >
    <div class="dialog-box">
      <div class="left-box">
        <div class="title">卡券管理</div>
        <div class="scroll-list">
          <div class="scroll-title">用户优惠券</div>
          <template v-if="currentUserCouponList.length">
            <div
              class="scroll-item"
              v-for="(item, index) in currentUserCouponList"
              :key="index"
              @click="handleSelect(index, item)"
            >
              <!-- 满减券 -->
              <img
                v-if="item.coupon_type?.value === 10"
                class="bg-img"
                src="@/assets/images/coupon/manjianquan-bg.png"
                alt=""
              />
              <!-- 现金券 -->
              <img
                v-if="item.coupon_type?.value === 30"
                class="bg-img"
                src="@/assets/images/coupon/zhekouquan-bg.png"
                alt=""
              />

              <!-- 已过期 -->
              <img
                class="bg-img"
                v-if="item.is_expire === 1"
                src="@/assets/images/coupon/yiguoqi-bg.png"
                alt=""
              />
              <!-- 已选中 -->
              <img
                class="choose-img"
                v-if="selectedActiveIndex === index"
                src="@/assets/images/coupon/chooseCoupon.png"
                alt=""
              />
              <div class="scroll-item-left">
                <div class="reduce-price">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff">￥</span>
                  <span v-else>￥</span>
                  <div v-if="item.coupon_type?.value === 30" style="color: #fff">
                    {{ item.reduce_price }}
                  </div>
                  <div v-else>{{ item.reduce_price }}</div>
                </div>
              </div>
              <div class="scroll-item-right">
                <div class="reduce-name">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff">{{
                    item.coupon_type?.text
                  }}</span>
                  <span v-else>{{ item.coupon_type?.text }}</span>
                </div>
                <div class="reduce-desc">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff"
                    >满{{ item.min_price }}可用</span
                  >
                  <span v-else>满{{ item.min_price }}可用</span>
                </div>
                <template v-if="item.is_expire === 0">
                  <div class="reduce-time">
                    <span v-if="item.coupon_type?.value === 30" style="color: #fff"
                      >有效期至{{ item.end_time }}</span
                    >
                    <span v-else>有效期至{{ item.end_time }}</span>
                  </div>
                </template>
                <div
                  class="over-time"
                  :style="{ color: item.coupon_type?.value === 30 ? '#fff' : '#7c0a03' }"
                  v-if="item.is_expire === 1"
                >
                  已过期
                </div>
                <div
                  class="over-time"
                  :style="{ color: item.coupon_type?.value === 30 ? '#fff' : '#7c0a03' }"
                  v-if="item.is_expire === 2"
                >
                  未到可用时间
                </div>
              </div>
            </div>
          </template>
          <!-- 加载中的骨架屏 -->
          <div v-if="userLoading" class="skeleton-container">
            <el-skeleton :rows="3" animated />
            <el-skeleton :rows="3" animated style="margin-top: 60px" />
          </div>
          <!-- 暂无数据的组件 -->
          <div class="no-data-box-one" v-if="!userLoading && currentUserCouponList.length === 0">
            <el-empty description="暂无用户优惠券" />
          </div>
          <!-- 显示多少张优惠券 -->
          <div class="position-coupon-count">
            <span v-if="currentUserCouponList.length">共{{ currentUserCouponList.length }}张</span>
          </div>
        </div>
      </div>
      <div class="right-box">
        <div class="scroll-list">
          <div class="scroll-title">门店优惠券</div>
          <template v-if="currentStoreCouponList.length">
            <div
              class="scroll-item"
              v-for="(item, index) in currentStoreCouponList"
              :key="index"
              @click="handleStoreSelect(index, item)"
            >
              <!-- 满减券 -->
              <img
                v-if="item.coupon_type?.value === 10"
                class="bg-img"
                src="@/assets/images/coupon/manjianquan-bg.png"
                alt=""
              />
              <!-- 现金券 -->
              <img
                v-if="item.coupon_type?.value === 30"
                class="bg-img"
                src="@/assets/images/coupon/zhekouquan-bg.png"
                alt=""
              />
              <!-- 已选中 -->
              <img
                class="choose-img"
                v-if="selectedStoreActiveIndex === index"
                src="@/assets/images/coupon/chooseCoupon.png"
                alt=""
              />
              <div class="scroll-item-left">
                <div class="reduce-price">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff">￥</span>
                  <span v-else>￥</span>
                  <div v-if="item.coupon_type?.value === 30" style="color: #fff">
                    {{ item.price || item.reduce_price }}
                  </div>
                  <div v-else>{{ item.reduce_price }}</div>
                </div>
              </div>
              <div class="scroll-item-right">
                <div class="reduce-name">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff">{{
                    item.coupon_type?.text
                  }}</span>
                  <span v-else>{{ item.coupon_type?.text }}</span>
                </div>
                <div class="reduce-desc">
                  <span v-if="item.coupon_type?.value === 10">满{{ item.min_price }}可用</span>
                </div>

                <div class="reduce-time">
                  <span v-if="item.coupon_type?.value === 30" style="color: #fff"
                    >有效期至{{ item.end_time }}</span
                  >
                  <span v-else>有效期至{{ item.end_time }}</span>
                </div>
              </div>
            </div>
          </template>
          <!-- 加载中的骨架屏 -->
          <div v-if="storeLoading" class="skeleton-container">
            <el-skeleton :rows="3" animated />
            <el-skeleton :rows="3" animated style="margin-top: 20px" />
          </div>
          <!-- 暂无数据的组件 -->
          <div
            class="no-data-box-one"
            v-else-if="!storeLoading && currentStoreCouponList.length === 0"
          >
            <el-empty description="暂无门店优惠券" />
          </div>
        </div>
        <!-- 操作按钮 -->
        <div class="button-box">
          <el-button
            type="primary"
            @click="handleBindCoupon"
            class="confirm-btn"
            :loading="loading"
            :disabled="selectedActiveIndex === -1 && selectedStoreActiveIndex === -1"
          >
            确定使用
          </el-button>
        </div>
      </div>
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import { userCouponList, shopCouponList, deskOrderBindCoupon } from '@/api/user'

interface Props {
  modelValue: boolean
  deskDetail?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: null
})

const emit = defineEmits<Emits>()

// 对话框显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

// 当前桌台详情
const currentDeskDetail = computed(() => props.deskDetail)

// 选中对应用户优惠券的索引
const selectedActiveIndex = ref(-1)

// 选中对应门店优惠券的索引
const selectedStoreActiveIndex = ref(-1)

// 当前用户的优惠券列表
const currentUserCouponList = ref<any[]>([])

// 当前门店的优惠券列表
const currentStoreCouponList = ref<any[]>([])

// 当前选中的优惠券id
const selectedCouponId = ref(0)

// 确认按钮加载状态
const loading = ref(false)
// 用户卡券加载状态
const userLoading = ref(false)
// 门店卡券加载状态
const storeLoading = ref(false)

// 选择用户优惠券
const handleSelect = (index: number, item: any) => {
  selectedActiveIndex.value = index
  selectedStoreActiveIndex.value = -1
  selectedCouponId.value = item.coupon_id
}

// 选择门店优惠券
const handleStoreSelect = (index: number, item: any) => {
  selectedStoreActiveIndex.value = index
  selectedActiveIndex.value = -1
  selectedCouponId.value = item.coupon_id
}

// 获取用户优惠券
const getUserCouponList = async () => {
  if (!currentDeskDetail.value?.user) return

  userLoading.value = true
  try {
    const datas = {
      user_id: currentDeskDetail.value.user.user_id
    }
    const res = await userCouponList(datas)
    if (res.status === 200) {
      currentUserCouponList.value = res?.data?.list || []
    }
  } catch (error) {
    console.error('获取用户优惠券列表失败:', error)
    ElMessage.error('获取用户优惠券列表失败')
  } finally {
    userLoading.value = false
  }
}

// 获取门店优惠券
const getShopCouponList = async () => {
  storeLoading.value = true
  try {
    const res = await shopCouponList()
    if (res.status === 200) {
      currentStoreCouponList.value = res?.data || []
    }
  } catch (error) {
    console.error('获取门店优惠券列表失败:', error)
    ElMessage.error('获取门店优惠券列表失败')
  } finally {
    storeLoading.value = false
  }
}

// 取消操作
const handleCancel = () => {
  dialogVisible.value = false
}

// 选择优惠券并绑定到当前桌台未结算的订单下面
const handleBindCoupon = async () => {
  if (selectedActiveIndex.value === -1 && selectedStoreActiveIndex.value === -1) return
  if (loading.value) return // 如果正在加载中，防止重复点击

  loading.value = true
  try {
    const datas = {
      // 桌台记录id
      record_id: currentDeskDetail.value.id,
      // 优惠券id(填0为取消优惠券)
      coupon_id: selectedCouponId.value,
      // (0:用户优惠券 1:店铺优惠券)
      type: selectedActiveIndex.value !== -1 ? '0' : '1'
    }

    const res = await deskOrderBindCoupon(datas)
    if (res.status === 200) {
      ElMessage.success('操作成功')
      // 通知父组件刷新数据
      emit('success')
      dialogVisible.value = false
    }
  } catch (error) {
    console.error('绑定优惠券失败:', error)
    ElMessage.error('绑定优惠券失败')
  } finally {
    loading.value = false
  }
}

// 监听对话框打开状态
watch(
  () => dialogVisible.value,
  val => {
    if (val) {
      // 获取用户优惠券
      if (currentDeskDetail.value?.user) {
        getUserCouponList()
      }
      // 获取门店优惠券
      getShopCouponList()
      // 重置选中用户优惠券的索引
      selectedActiveIndex.value = -1
      selectedStoreActiveIndex.value = -1
    }
  }
)
</script>

<style scoped lang="scss">
/* 滚动条整体的大小 */
.scroll-list::-webkit-scrollbar {
  width: 6px !important;
  height: 0 !important;
}
/* 滚动条里滑动的滑块 */
.scroll-list::-webkit-scrollbar-thumb {
  height: 200px !important;
  background: #d2d2d2 !important;
  border-radius: 3px !important;
}
/* 滚动条底层样式 */
.scroll-list::-webkit-scrollbar-track {
  display: none !important;
}

.dialog-box {
  display: flex;
  flex-direction: row;
  .left-box {
    width: 477px;
    .title {
      font-size: 20px;
      font-family:
        PingFang SC,
        PingFang SC-Bold;
      font-weight: 700;
      text-align: left;
      color: #333333;
    }
    .scroll-list {
      position: relative;
      box-sizing: border-box;
      width: 477px;
      height: 635px;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      padding: 0 30px 100px 30px;
      margin-top: 30px;
      overflow-y: auto;
      overflow-x: hidden;

      .scroll-title {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
        margin-top: 20px;
      }
      .scroll-item {
        position: relative;
        box-sizing: border-box;
        width: 417px;
        height: 147px;
        padding-left: 20px;
        display: flex;
        align-items: center;
        margin-top: 60px;
        cursor: pointer;
        .bg-img {
          position: absolute;
          top: 0;
          left: 0;
          z-index: 1;
          width: 417px;
          height: 147px;
        }
        .choose-img {
          position: absolute;
          top: 0;
          right: 0;
          z-index: 2;
          width: 75px;
          height: 75px;
        }
        .scroll-item-left {
          position: relative;
          z-index: 3;
          margin-right: 50px;
          .reduce-price {
            display: flex;
            align-items: flex-end;
            font-size: 16px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
            > div {
              font-size: 50px;
              line-height: 20rpx;
            }
          }
          .discount-price {
            display: flex;
            align-items: flex-end;
            width: 50px;
            font-size: 20px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;
            > div {
              font-size: 50px;
            }
          }
        }
        .scroll-item-right {
          position: absolute;
          right: 20px;
          z-index: 3;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .reduce-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }
          .reduce-desc {
            margin-top: 6px;
            font-size: 18px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }
          .reduce-time {
            margin-top: 12px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #7c0a03;
          }
          .discount-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;
          }
          .over-time,
          .discount-time {
            margin-top: 24px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #c89359;
          }
        }
      }
      .position-coupon-count {
        position: fixed;
        z-index: 4;
        bottom: 125px;
        left: 60px;
        height: 80px;
        width: 460px;
        background: #ffffff;
        box-sizing: border-box;
        padding-left: 30px;
        display: flex;
        align-items: center;
        font-size: 14px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
      }
    }
  }
  .right-box {
    width: 477px;
    margin-left: 70px;
    margin-top: 50px;
    .scroll-list {
      position: relative;
      box-sizing: border-box;
      width: 477px;
      height: 635px;
      border: 1px solid #d2d2d2;
      border-radius: 12px;
      padding: 0 30px 20px 30px;
      overflow-y: auto;
      .scroll-title {
        font-size: 16px;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: left;
        color: #333333;
        margin-top: 20px;
      }
      .scroll-item {
        position: relative;
        box-sizing: border-box;
        width: 417px;
        height: 147px;
        padding-left: 20px;
        display: flex;
        align-items: center;
        margin-top: 20px;
        cursor: pointer;
        .bg-img {
          position: absolute;
          top: 0;
          left: 0;
          z-index: 1;
          width: 417px;
          height: 147px;
        }
        .choose-img {
          position: absolute;
          top: 0;
          right: 0;
          z-index: 2;
          width: 75px;
          height: 75px;
        }
        .scroll-item-left {
          position: relative;
          z-index: 3;
          margin-right: 50px;
          .reduce-price {
            display: flex;
            align-items: flex-end;
            font-size: 16px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
            padding-left: 10px;
            > div {
              line-height: 40px;
              font-size: 40px;
            }
          }
          .discount-price {
            display: flex;
            align-items: flex-end;
            width: 50px;
            font-size: 20px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;
            > div {
              font-size: 50px;
            }
          }
        }
        .scroll-item-right {
          position: absolute;
          right: 22px;
          z-index: 3;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          .reduce-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }
          .reduce-desc {
            margin-top: 6px;
            font-size: 18px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #7c0a03;
          }
          .reduce-time {
            margin-top: 12px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #7c0a03;
          }
          .discount-name {
            font-size: 30px;
            font-family:
              PingFang SC,
              PingFang SC-Bold;
            font-weight: 700;
            color: #ffe2c2;
          }
          .over-time,
          .discount-time {
            margin-top: 24px;
            font-size: 14px;
            font-family:
              PingFang SC,
              PingFang SC-Regular;
            font-weight: 400;
            color: #c89359;
          }
        }
      }
    }
    .button-box {
      height: 52px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 30px;
      .confirm-btn {
        width: 336px;
        height: 52px;
        background-color: #7c0a03 !important;
        border-color: #7c0a03 !important;
        border-radius: 12px !important;
        font-size: 18px !important;
        font-family:
          PingFang SC,
          PingFang SC-Regular;
        font-weight: 400;
        text-align: center;
        color: #ffffff !important;
        line-height: 20px;

        &:hover:not(:disabled) {
          background-color: #8c1a13 !important;
          border-color: #8c1a13 !important;
        }

        &:disabled {
          background-color: #999 !important;
          border-color: #999 !important;
          cursor: not-allowed;
        }
      }
    }
  }
}
.no-data-box-one {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 170px;
  margin-top: 150px;
  width: 100%;
}

.skeleton-container {
  padding: 20px;
  margin-top: 60px;
}
</style>
<style lang="scss">
.coupon-management-dialog {
  max-height: unset !important;
}
</style>
